package com.example.homework2.controller;

import com.example.homework2.model.Student;
import com.example.homework2.service.impl.impe.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class StudentController {
    @Autowired
    StudentService studentService;


@CrossOrigin(origins = "http://localhost:8080")
    @GetMapping("/student")
    public List<Student> getAllStudent() {
        return studentService.list();
    }

    @GetMapping("/student/{id}")
    public Student getStudentById(@PathVariable("id") Integer id) {
        Student student=studentService.getById(id);
        return student;
    }

    @PostMapping("/student")
    public String addStudent(@RequestBody Student student) {
        if (studentService.save(student))
        {
            return "数据插入成功";
        }
        else {
            return "数据插入失败";
        }
    }

    @PutMapping("/student")
    public String updateStudent(@RequestBody Student student) {
        if (studentService.updateById(student))
        {
            return "数据更新成功";
        }
        else {
            return "数据更新失败";
        }
    }

    @DeleteMapping("/student")
    public String deleteUserById(Integer id){
        if (studentService.removeById(id)){
            return "删除用户成功！";
        }
        else return "删除用户失败！";
    }
}
